package com.ericsson.rm.controller;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.ericsson.rm.form.SearchResourceCommand;
import com.ericsson.rm.response.RoleDetailResponse;
import com.ericsson.rm.response.SkillDetailResponse;
import com.ericsson.rm.service.ResourceSearchService;
import com.ericsson.rm.service.SkillRoleResourceService;
import com.ericsson.rm.util.Utility;
import com.ericsson.rm.validator.ResourceSearchValidator;
import com.ericsson.rm.vo.ResourceSearchInputVO;
import com.ericsson.rm.vo.ResourceSearchResponseVO;

@Controller
@RequestMapping("/SearchResource")
public class ResourceSearchController {
	private final static Logger logger = LoggerFactory.getLogger(ResourceSearchController.class);
	private final static String CLASS_NAME = ResourceSearchController.class.getSimpleName();
	private final static String PAGE_SEARCH_RESOURCE_FORM = "SearchResource";
	
	private ResourceSearchValidator resourceSearchValidator;
	
	@Autowired
	private SkillRoleResourceService skillRoleResourceService;
	
	@Autowired
	private ResourceSearchService resourceSearchService;
	
	@Autowired
	public ResourceSearchController(ResourceSearchValidator resourceSearchValidator){
		if(logger.isDebugEnabled()){
			logger.debug(CLASS_NAME+" constructor entering...");
		}
		
		this.resourceSearchValidator = resourceSearchValidator;
	}
	
	@RequestMapping(method = RequestMethod.GET)
	public String initForm(ModelMap model){
		final String METHOD_NAME = CLASS_NAME + ".initForm() ";
		if(logger.isDebugEnabled()){
			logger.debug(METHOD_NAME+"entering...");
		}
				
		SearchResourceCommand searchResourceCmd = new SearchResourceCommand();
		model.addAttribute("SearchResourceCommand", searchResourceCmd);
						
		return PAGE_SEARCH_RESOURCE_FORM;
	}
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		final String METHOD_NAME = CLASS_NAME + ".initBinder() ";
		if(logger.isDebugEnabled()){
			logger.debug(METHOD_NAME+"entering...");
		}
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
	
	@ModelAttribute("roleNames")
	public Map<String,String> populateRoleNames() {
		final String METHOD_NAME = CLASS_NAME + ".populateRoleNames() ";
		if(logger.isDebugEnabled()){
			logger.debug(METHOD_NAME+"entering...");
		}
		
		Map<String,String> roleNames = new LinkedHashMap<String,String>();
		List<RoleDetailResponse> roleList = skillRoleResourceService.fetchRoleDetail();
		
		if(Utility.isNotEmpty(roleList)){
			Iterator<RoleDetailResponse> itr = roleList.iterator();
			
			while(itr.hasNext()){
				RoleDetailResponse role = itr.next();
				
				roleNames.put(role.getJobStage(), role.getRoleName());
			}
		}
		
		if(logger.isDebugEnabled()){
			logger.debug(METHOD_NAME+"RoleNames : "+roleNames);
		}
		
		return roleNames;
	}
	
	@ModelAttribute("skillNames")
	public Map<String,String> populateSkillNames() {
		final String METHOD_NAME = CLASS_NAME + ".populateSkillNames() ";
		if(logger.isDebugEnabled()){
			logger.debug(METHOD_NAME+"entering...");
		}
		
		Map<String,String> skillNames = new LinkedHashMap<String,String>();
		List<SkillDetailResponse> skillList = skillRoleResourceService.fetchSkillDetail();
		
		if(Utility.isNotEmpty(skillList)){
			Iterator<SkillDetailResponse> itr = skillList.iterator();
			
			while(itr.hasNext()){
				SkillDetailResponse skill = itr.next();
				
				skillNames.put(skill.getSkillId(), skill.getSkillDescription());
			}
		}
		
		if(logger.isDebugEnabled()){
			logger.debug(METHOD_NAME+"SkillNames : "+skillNames);
		}
		
		return skillNames;
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView search(@ModelAttribute("SearchResourceCommand") SearchResourceCommand searchResourceCommand, BindingResult result, SessionStatus status) {
		final String METHOD_NAME = CLASS_NAME + ".search() ";
		if(logger.isDebugEnabled()){
			logger.debug(METHOD_NAME+"entering...");
			logger.debug(METHOD_NAME+"SearchResourceCommand: "+Utility.displayBean(searchResourceCommand));
		}
		
		resourceSearchValidator.validate(searchResourceCommand, result);
			
		if (result.hasErrors()) {
			//return PAGE_SEARCH_RESOURCE_FORM;
			return new ModelAndView(PAGE_SEARCH_RESOURCE_FORM);
		} else {
			
			ResourceSearchInputVO resourceSearchInput = (ResourceSearchInputVO) Utility.copyBean(searchResourceCommand, ResourceSearchInputVO.class);
			if(logger.isDebugEnabled()){
				logger.debug(METHOD_NAME+"ResourceSearchInputVO: "+Utility.displayBean(resourceSearchInput));
			}
			List<ResourceSearchResponseVO> resourceAllocationDetails = resourceSearchService.searchResourceAllocation(resourceSearchInput);
			
			Map<String, Object> myModel = new HashMap<String, Object>(); 
			myModel.put("resourceAllocationDetails", resourceAllocationDetails);
			 
			status.setComplete();
			//return PAGE_SEARCH_RESOURCE_FORM;
			return new ModelAndView(PAGE_SEARCH_RESOURCE_FORM, "result", myModel); 
		}
	}
}
